package com.wbl.train.member.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wbl.train.common.member.dto.TicketDto;
import com.wbl.train.common.member.entity.TicketEntity;
import com.wbl.train.common.page.PageVo;
import com.wbl.train.member.mapper.TicketMapper;
import com.wbl.train.member.service.TicketService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 车票 服务实现类
 * @author 王博纶
 * @since 2024-03-07
 */
@Service
public class TicketServiceImpl extends ServiceImpl<TicketMapper, TicketEntity> implements TicketService {

    @Override
    public PageVo<TicketEntity> queryTicket(TicketDto ticketDto) {
        // 获取分页所需要的参数
        int page = ticketDto.getPage();
        int limit = ticketDto.getLimit();

        LambdaQueryWrapper<TicketEntity> ticketWrapper = new LambdaQueryWrapper<TicketEntity>()
                .eq(ObjectUtil.isNotNull(ticketDto.getMemberId()),TicketEntity::getMemberId,ticketDto.getMemberId())
                .orderByAsc(TicketEntity::getId);
        //创建分页对象
        IPage<TicketEntity>ticketPage = new Page<>(page,limit);
        //调用分页方法
        IPage<TicketEntity> ticketPageData = page(ticketPage,ticketWrapper);
        // 获取查询出来的数据集合
        List<TicketEntity> records = ticketPageData.getRecords();
        // 将数据塞到PageVo类中并返回出去
        return new PageVo<TicketEntity>()
                .setDataList(records)
                .setPageSize(limit).setCurrentPage(page)
                .setTotal((int) ticketPageData.getTotal())
                .setPages((int) ticketPageData.getPages());
    }
}
